<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
$shop=$this->plugin_setting('shop');
$return['shopset']=$shop;
$keywords=$this->plugin_setting('search');
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['id']);
	$verifycode=$_RQ['verifycode'];
	if (empty($verifycode)) {
			exi('请输入核销码!','error','jy_weishop/verify.index/');
	}
	$order_verify=$this->pdo_get('order_verify',array('verify_code'=>$verifycode));
	if (empty($order_verify)) 
	{
		exi('核销码错误,无权核销','error','jy_weishop/verify.index/');   
	}
	if($orderid){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$uniacid,'id'=>$orderid));
	}else{
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$uniacid,'id'=>$order_verify['orderid']));
	}
	//商品独立核销门店：
	$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$storeids=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$uniacid,'id'=>$order_verify['goodsid']),'storeids');
	$storeids=$storeids?json_decode($storeids,true):array();
	if($storeids && count($storeids)>0){
		foreach($storeids as $sv){
			$prams['storeid']=$sv['id'];
			$saler=pdo_get('jy_weishop_store_worker',$prams);
			if($saler){
				break;
			}
		}
	}else{
		//核销店员身份
		if($order['storeid']){
			//$prams['storeid']=$order['storeid'];
		}
		$saler=pdo_get('jy_weishop_store_worker',$prams);
	}
	if (empty($saler)) 
	{
		exi('您不是该店的核销员或不属于该商品的核销门店,无权核销！','error','jy_weishop/verify.index/');   
	}
	if (empty($order)) {
			exi('订单不存在!','error','jy_weishop/verify.index/');
	}
	if($order['status']==4){
		exi('订单已关闭，不能核销！','error','jy_weishop/verify.index/');
	}elseif($order['status']==5){
		exi('订单已申请退款，不能核销！','error','jy_weishop/verify.index/');
	}elseif($order['status']==5){
		exi('订单已退款，不能核销！','error','jy_weishop/verify.index/');
	}
	if ($order_verify['status'] ==2 || ($order_verify['endtime'] && $order_verify['endtime'] < TIMESTAMP)) {	
		exi('该核销码已失效，使用期限已过!','error','jy_weishop/verify.index/');		
	}
	if ($order_verify['status'] ==1) {	
		exi('该核销码已被使用!','error','jy_weishop/verify.index/');		
	}
	
	
	$order_goods=$this->pdo_getall('order_goods',array('orderid'=>$order['id']));
	foreach($order_goods as &$item){
		$goods=$this->pdo_get('goods',array('id'=>$item['goodsid']),array('title','image'));
		//商品规格
		if($item['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$item['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goods['selespename']=implode("，",$specs);
		}
		$item=array_merge($goods,$item);
	}
	$return['post']=array(
		'orderid'=>$order['id'],
		'verify_code'=>$verifycode,
	);
	$return['order']=$order;
	$return['order_goods']=$order_goods;
	set_html('title','核销');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$verify_code=trim($post['verify_code']);
	$this->model('order')->order_verify($verify_code,$_SESSION['uid']);
	exi('核销成功！','','jy_weishop/verify.index/');
	
}